import { ref, computed } from 'vue'

export function useEditTodo(todoListRef) {
  let cacheTodo = null
  const editingTodoRef = ref(null)
  const clickEdit = (todo) => {
    cacheTodo = todo.content
    editingTodoRef.value = todo
  }
  const fulfilledEdit = () => {
    editingTodoRef.value = null
  }
  const cancelEdit = (todo) => {
    console.log(cacheTodo)
    editingTodoRef.value = null
    todo.content = cacheTodo
  }
  const allTodosRef = computed(() => {
    return todoListRef.value.filter(item => !item.isFinish).length === 0
  })
  const changeAlltodo = (isdone) => {
    console.log(isdone)
    todoListRef.value.forEach(item => item.isFinish = isdone)
  }
  return {
    editingTodoRef,
    clickEdit,
    fulfilledEdit,
    cancelEdit,
    allTodosRef,
    changeAlltodo
  }
}